package com.ruoyi.rule.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.rule.domain.RuleAction;
import com.ruoyi.rule.domain.RuleCondition;
import com.ruoyi.rule.service.IRuleActionService;
import com.ruoyi.rule.service.IRuleConditionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ruoyi.rule.mapper.RuleMapper;
import com.ruoyi.rule.domain.Rule;
import com.ruoyi.rule.service.IRuleService;
import org.springframework.util.CollectionUtils;

/**
 * 规则Service业务层处理
 *
 * @author changkang
 * @date 2025-05-12
 */
@Service
public class RuleServiceImpl implements IRuleService {
    @Resource
    private RuleMapper ruleMapper;

    @Resource
    IRuleConditionService ruleConditionService;

    @Resource
    IRuleActionService ruleActionService;

    /**
     * 查询规则
     *
     * @param id 规则主键
     * @return 规则
     */
    @Override
    public Rule selectRuleById(Long id)
    {
        return ruleMapper.selectRuleById(id);
    }

    /**
     * 查询规则列表
     *
     * @param rule 规则
     * @return 规则
     */
    @Override
    public List<Rule> selectRuleList(Rule rule)
    {
        return ruleMapper.selectRuleList(rule);
    }

    @Override
    public List<Rule> selectRuleListByDevice(String productKey, String ChannelKey, String deviceKey) {
        // 查询启用并在生效时间段内的规则,生效时间段后面再加
        Rule rule = new Rule();
        rule.setProductKey(productKey);
        rule.setDeviceKey(deviceKey);
        rule.setChannelKey(ChannelKey);
        rule.setStatus("1");
        rule.setEffectiveStartTime(new Date());
        List<Rule> rules = selectRuleList(rule);
        if(CollectionUtils.isEmpty(rules)){
            return null;
        }

        rules.parallelStream().forEach(ruleItem -> {
            // 查询规则
            RuleCondition ruleCondition = new RuleCondition();
            ruleCondition.setRuleId(ruleItem.getId());
            ruleItem.setConditions(ruleConditionService.selectRuleConditionList(ruleCondition));
            // 查询动作，并添加动作
            RuleAction ruleAction = new RuleAction();
            ruleAction.setRuleId(ruleItem.getId());
            ruleItem.setActions(ruleActionService.selectRuleActionList(ruleAction));
        });
        // 查询规则信息
        return rules;
    }


    /**
     * 新增规则
     *
     * @param rule 规则
     * @return 结果
     */
    @Override
    public int insertRule(Rule rule)
    {
        rule.setCreateTime(new Date());
        return ruleMapper.insertRule(rule);
    }

    /**
     * 修改规则
     *
     * @param rule 规则
     * @return 结果
     */
    @Override
    public int updateRule(Rule rule)
    {
        return ruleMapper.updateRule(rule);
    }

    /**
     * 批量删除规则
     *
     * @param ids 需要删除的规则主键
     * @return 结果
     */
    @Override
    public int deleteRuleByIds(Long[] ids)
    {
        return ruleMapper.deleteRuleByIds(ids);
    }

    /**
     * 删除规则信息
     *
     * @param id 规则主键
     * @return 结果
     */
    @Override
    public int deleteRuleById(Long id)
    {
        return ruleMapper.deleteRuleById(id);
    }
}
